<?php


namespace app\common\model\mysql;


use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

class User extends Model
{
    // 自动生成写入时间
    protected $autoWriteTimestamp = true;
    public function getUserByPhoneNumber($phoneNumber){
        if (empty($phoneNumber)){
            return false;
        }

        $where = [
            'phone_number' => trim($phoneNumber)
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 根据主键ID更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateByID($id,$data){
        $id = intval($id);
        if (empty($id) || empty($data) || !is_array($data)) {
            return false;
        }

        $where = [
            'id' => $id
        ];
        return $this->where($where)->save($data);
    }

    /**
     * 根据id获取用户数据
     * @param $id
     * @return array|false|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getUserById($id){
        $id = intval($id);
        if (!$id){
            return false;
        }
        return $this->find($id);
    }

    public function getUserByUsername($username){
        if (empty($username)){
            return false;
        }

        $where = [
            'username' => trim($username)
        ];
        $result = $this->where($where)->find();
        return $result;
    }
}